//
//  GoogleSignInHelper.m
//  Cycling
//
//  Created by cjg.mini on 2023/4/4.
//

#import "GoogleSignInHelper.h"
 
#import <GoogleSignIn/GIDSignIn.h>
#import <GoogleSignIn/GIDGoogleUser.h>
#import <GoogleSignIn/GIDConfiguration.h>
#import <GoogleSignIn/GIDProfileData.h>
#import <GoogleSignIn/GIDSignInResult.h>
#import <GoogleSignIn/GIDToken.h>
#import "UserRequest.h"
#import <FirebaseCore/FirebaseCore.h>

static NSString * const kClientID = @"com.googleusercontent.apps.199159291456-fe788mdkkdm6h7so1hu72gj6ri6a0uc7";
 
@implementation GoogleSignInHelper {
    // Configuration options for GIDSignIn.
    GIDConfiguration *_configuration;
}
 
static GoogleSignInHelper *instance;
 
+ (GoogleSignInHelper *)shareInstance
{
    if (!instance) {
        instance = [[GoogleSignInHelper alloc] init];
    }
    return instance;
}
 
- (id) init
{
    if (self = [super init]) {
        if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) {
            NSArray* array = [[UIApplication sharedApplication] windows];
            UIWindow* window = [array objectAtIndex:0];
            _viewController = (UIViewController*)[[[window subviews] objectAtIndex:0]nextResponder];
        } else {
            _viewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
        }
        
//        _configuration = [[GIDConfiguration alloc] initWithClientID:kClientID];
        _configuration = [[GIDConfiguration alloc] initWithClientID:[FIRApp defaultApp].options.clientID];
        
    }
    
    return  self;
}
 
-(void)signIn:(void(^)(BOOL ret, NSString *token))resultBlock withPresentingVC:(nonnull UIViewController *)presentingVC
{
    [self signIn:resultBlock withPresentingVC:presentingVC showWaitBlock:^{
        
    }];
}
- (void)signIn:(void(^)(BOOL ret, NSString *token))resultBlock withPresentingVC:(nonnull UIViewController *)presentingVC showWaitBlock:(void(^)(void))showWaitBlock
{
    GIDSignIn *signInManager = [GIDSignIn sharedInstance];
    signInManager.configuration = _configuration;
    [signInManager signOut];
    if(signInManager.hasPreviousSignIn) {
        [signInManager disconnectWithCompletion:^(NSError *error) {
            [self signInAfterChecked:resultBlock withPresentingVC:presentingVC showWaitBlock:showWaitBlock];
        }];
        return;
    }
    [self signInAfterChecked:resultBlock withPresentingVC:presentingVC showWaitBlock:showWaitBlock];
}

- (void)signInAfterChecked:(void(^)(BOOL ret, NSString *token))resultBlock withPresentingVC:(nonnull UIViewController *)presentingVC showWaitBlock:(void(^)(void))showWaitBlock
{
    GIDSignIn *signInManager = [GIDSignIn sharedInstance];
    signInManager.configuration = _configuration;
    [signInManager signInWithPresentingViewController:presentingVC completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) {
        if (error != nil) {
            if (error.code == kGIDSignInErrorCodeHasNoAuthInKeychain) {
                NSLog(@"***The user has not signed in before or they have since signed out.");
            } else {
                NSLog(@"***%@", error.localizedDescription);
            }
            if(resultBlock){
                resultBlock(NO,nil);
            }
        } else {
            NSLog(@"*** -- %@", signInResult);
            if(showWaitBlock) {
                showWaitBlock();
            }
            NSString *userId = signInResult.user.userID;                  // For client-side use only!
            NSString *idToken = signInResult.user.idToken.tokenString; // Safe to send to the server
            NSString *fullName = signInResult.user.profile.name;
            NSString *givenName = signInResult.user.profile.givenName;
            NSString *familyName = signInResult.user.profile.familyName;
            NSString *email = signInResult.user.profile.email;
            if(resultBlock){
                resultBlock(YES,idToken);
            }
        }
    }];
}
 
-(void)signOut
{
    [GIDSignIn.sharedInstance signOut];

}

- (BOOL)currentLoginState {
    return [GIDSignIn sharedInstance].currentUser;
}
 
@end

